package com.lantosec.hmrp.core.validation;

import com.lantosec.hmrp.core.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * isMobile注解校验代码
 *
 * @author 杨成宇
 * @version 1.0
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
    private boolean required = false;

    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        // value为空时进行校验
        if (StringUtils.isBlank(value)) {
            return !required;
        } else {
            // 不为空时进行校验
            return ValidatorUtils.isMobile(value);
        }
    }
}
